//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SqlQueryBuilderConditionItem.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.ComponentModel;

namespace Smart.Common.Entities
{
	/// <summary>
	/// QueryBuilder Query Condition Item
	/// </summary>
	[Localizable(false)]
	public class SqlQueryBuilderConditionItem<TExecutorType> : SqlQueryBuilderItem<TExecutorType>
		where TExecutorType : class
	{
		/// <summary>
		/// Field
		/// </summary>
		public string Field { get; set; }

		/// <summary>
		/// Value
		/// </summary>
		public object Value { get; set; }

		/// <summary>
		/// QueryBuilder Query Condition Item
		/// </summary>
		/// <param name="field">Field name</param>
		/// <param name="op">Operation</param>
		/// <param name="value">Value</param>
		public SqlQueryBuilderConditionItem(string field, string op, object value)
			: base(op)
		{
			this.Field = field;
			this.Value = value;
		}

		/// <summary>
		/// Return Cache code
		/// </summary>
		public override int GetCacheCode()
		{
			return
				(Field == null ? 0 : Field.GetHashCode()) ^
				(Op == null ? 0 : Op.GetHashCode()) ^
				(Value == null ? 0 : Value.GetHashCode());
		}

		/// <summary>
		/// Format QueryBuilder statement
		/// </summary>
		public override string FormatQuery()
		{
			string fieldName = (Query == null ? Field : Query.Meta.FieldName(Field));

			return string.Format("{0} {1} {2}", fieldName, Op, Value.IncapsulateSql());
		}
	}
}